<?php
namespace app\admin\model;


use think\facade\Db;
use think\facade\Request;
use think\Model;
use think\model\concern\SoftDelete;

class Gcategory extends Model
{
    use SoftDelete;
    protected $pk = 'cate_id';
    protected $deleteTime = 'delete_time';

    public static function getCateGory($parent_id)
    {
        $list = Gcategory::where('parent_id','=',$parent_id)
            ->field(true)
            ->order('sort_order')
            ->select();
        return $list;
    }
    public static function getNextCate($list)
    {
        foreach ($list as $k=>&$v){
            $v['next_cate'] = Gcategory::where('parent_id','=',$v['cate_id'])
                ->value('cate_id');
        }
        return $list;
    }
    //商品列表分类查询
    public static function getGoodsCate()
    {
        //调用查询
        $list = Gcategory::select()->toArray();
        //存放下拉列表的新数据
        $arr = [];
        //循环一级分类
        foreach ($list as $v){
            if ($v['parent_id']==0){
                //将一级分类赋值到$arr[]中
                $arr[$v['cate_id']] = $v;
                $arr[$v['cate_id']]['next'] = self::getGoodsCates($list,$v['cate_id']);
            }
        }

        return $arr;
    }
    //递归
    public static function getGoodsCates($list,$parent_id)
    {
        $arr = [];
        foreach ($list as $vv){
            if ($parent_id==$vv['parent_id']){
                //下一级分类赋值到$arr
                $arr[$vv['cate_id']] = $vv;
                $arr[$vv['cate_id']]['next'] = self::getGoodsCates($list,$vv['cate_id']);
            }
        }
        return $arr;
    }
    //分类列表判断显示
    public static function getIfShowNextAttr($v,$data)
    {
        if ($data['if_show']==1){
            return "<font style='color: red'>显示</font>";
        }else{
            return "<font style='color: green'>不显示</font>";
        }
    }
    //分类添加
    public static function addCateInfo($add)
    {
        try{
            $res = Gcategory::strict(true)->save($add);
            $msg = '添加成功';
        }catch (\Exception $e){
            $res = false;
            $msg = '添加失败';
        }
        return [$res,$msg];
    }
    //分类修改
    public static function editCateInfo($get,$edit)
    {
        if (Request::isPost()){
            try{
                $res = Gcategory::update($edit);
                $msg = '修改成功';
            }catch (\Exception $e){
                $res = false;
                $msg = '修改失败';
            }
            return [$res,$msg];
        }else{
            $list = Gcategory::where('cate_id','=',$get['id'])->find()->toArray();
            return $list;
        }
    }
    //分类删除
    public static function delCateInfo($get)
    {
        $col_id = Gcategory::where('parent_id','=',$get['id'])->column('parent_id');
        $cid = Goods::where('cate_id','=',$get['id'])->column('cate_id');
        try{
            if (empty($col_id) && empty($cid)){
                $res = Gcategory::destroy($get['id']);
                $msg = "删除成功";
            }else{
                $res = false;
                $msg = '该值正在使用';
            }
        }catch (\Exception $e){
            $res = false;
            $msg = $e->getMessage();
        }
        return [$res,$msg];
    }







}